package com.briup.srms.sms.web.controller;

import java.util.Arrays;
import java.util.Date;

import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.briup.srms.sms.bean.Leave;
import com.briup.srms.sms.bean.extend.LeaveExtend;
import com.briup.srms.sms.service.ILeaveService;
import com.jacky.base.utils.Message;
import com.jacky.base.utils.MessageUtil;
import com.jacky.base.utils.PageVM;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @program: sms
 * @description: 请假控制器类
 * @author: jacky
 * @create: 2020-12-13 14:41
 **/
@Api(description="请假相关接口")
@Validated
@RestController
@RequestMapping("/leave")
public class LeaveController {
	
	@Autowired
	private ILeaveService leaveService;
	
	@ApiOperation(value = "分页多条件获取请假信息")
	@GetMapping("pageQuery")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query"),
		@ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, paramType = "query"),
		@ApiImplicitParam(name = "studentId", value = "学生编号", paramType = "query"),
		@ApiImplicitParam(name = "clazzId", value = "班级编号", paramType = "query")
	})
	public Message pageQuery(int page, int pageSize,Long studentId,Long clazzId) {
		PageVM<LeaveExtend> pageVM = leaveService.pageQuery(page, pageSize,studentId,clazzId);
		return MessageUtil.success(pageVM);
	}

	@ApiOperation(value = "通过id删除")
	@ApiImplicitParams(@ApiImplicitParam(name = "id", value = "主键", paramType = "query", required = true))
	@GetMapping("deleteById")
	public Message deleteById(long id) {
		leaveService.deleteById(id);
		return MessageUtil.success("删除成功");
	}

	@ApiOperation(value = "取消请假", notes = "")
	@GetMapping("cancelApply")
	public Message cancelApply(@NotNull Long id) {
		leaveService.cancelApply(id);
		return MessageUtil.success("取消成功");
	}
	
	@ApiOperation(value = "审核请假", notes = "")
	@PostMapping("checkApply")
	public Message checkApply(String status,Long[] leaveIds,String reason) {
		leaveService.checkApply(status,leaveIds,reason);
		return MessageUtil.success("操作成功");
	}
	
	@ApiOperation(value = "申请请假", notes = "")
	@PostMapping("apply")
	public Message apply(String reason,Long[] applyDate,String[] applyStages,Long studentId,String leavePaper,String scrip) {
		Leave leave = new Leave();
		leave.setApplyTime(new Date().getTime());
		leave.setBeginTime(applyDate[0]);
		leave.setFinishTime(applyDate[1]);
		leave.setReason(reason);
		leave.setStatus("未审核");
		leave.setType("请假");
		leave.setLeavePaper(leavePaper);
		leave.setScrip(scrip);
		leave.setComment(Arrays.toString(applyStages));
		leave.setStudentId(studentId);
		leaveService.saveOrUpdate(leave);
		return MessageUtil.success("请假成功");
	}
}
